package fr.cie.blog.modeldao.hib;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import fr.cie.blog.beans.Article;
import fr.cie.blog.beans.Comment;
import fr.cie.blog.beans.User;
import fr.cie.blog.domainservice.IServiceComment;

/**
 * @author AetM
 * 
 */
public class CommentDaoImplHib extends HibernateDaoSupport implements
		IServiceComment {
	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger.getLogger(CommentDaoImplHib.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#save(java.lang.Object)
	 */
	@Override
	public final int save(final Comment comment) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(comment);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Comment error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int update(final Comment comment) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(comment);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Comment error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#delete(java.lang.Object)
	 */
	@Override
	public final int delete(final Comment comment) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.delete(comment);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Comment error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findAll()
	 */
	@Override
	public final List<Comment> findAll() {
		@SuppressWarnings("unchecked")
		final List<Comment> list = (List<Comment>) getHibernateTemplate().find(
				"from Comment");
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceComment#findByArticle(fr.cie.blog.beans
	 * .Article)
	 */
	@Override
	public final List<Comment> findByArticle(final Article pArticle) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findById(int)
	 */
	@Override
	public final Comment findById(final long pIdentifier) {
		return getHibernateTemplate().get(Comment.class, pIdentifier);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceComment#findByUser(fr.cie.blog.beans
	 * .User)
	 */
	@Override
	public final List<Comment> findByUser(final User pUser) {
		// TODO Auto-generated method stub
		return null;
	}

}